﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO.IsolatedStorage;

namespace BeachRunner.Utility
{
    /// <summary>
    /// Schreibt und liest Werte in und aus den Telefon Application Settings
    /// 
    /// Author: Mathias Müller
    /// </summary>
    public class Config
    {
        private Boolean debug;
        public Boolean Debug
        {
            get { return debug; }
            set { debug = value; }
        }

        private Boolean fullscreen;
        public Boolean Fullscreen
        {
            get { return fullscreen; }
            set { fullscreen = value; }
        }

        private Boolean? sound_effects;
        public Boolean Sound_effects
        {
            get 
            {
                if (sound_effects == null)
                    sound_effects = Read("sound_effects");
                return sound_effects.GetValueOrDefault(); 
            }
            set 
            {
                sound_effects = value;
                Write("sound_effects", value);
            }
        }

        private Boolean? sound_background;
        public Boolean Sound_background
        {
            get
            {
                if (sound_background == null)
                    sound_background = Read("sound_background");
                return sound_background.GetValueOrDefault();
            }
            set
            {
                sound_background = value;
                Write("sound_background", value);
            }
        }

        public Config()
        {
            debug = false;
            fullscreen = true;
            sound_effects = null;
            sound_background = null;
        }

        /// <summary>
        /// Wert aus Application Settings auslesen
        /// </summary>
        /// <param name="s">Name des Auszulesenden Parameters</param>
        /// <returns>Wert</returns>
        private Boolean Read(String s)
        {
            Boolean tmp;
            if (IsolatedStorageSettings.ApplicationSettings.TryGetValue<Boolean>(s, out tmp))
                return tmp;
            else return false;
        }

        /// <summary>
        /// Schreibe einen Wert in die Application Settings, wenn er schon vorhanden ist wird er überschrieben.
        /// </summary>
        /// <param name="s">Keyname</param>
        /// <param name="value">Keywert</param>
        /// <returns>Erfolgreich oder nicht</returns>
        private Boolean Write(String s, Boolean value)
        {
            try
            {
                IsolatedStorageSettings.ApplicationSettings.Remove(s);
                IsolatedStorageSettings.ApplicationSettings.Add(s, value);
                IsolatedStorageSettings.ApplicationSettings.Save();

                return true;
            }
            catch(Exception)
            { return false; }
        }
    }
}
